<template>
  <div class="content" @click.stop="aotuTab=false, aotuTab1=false">
    <div class="dropdown">
      <jc-button
        :btn-arr="['reload', 'save', 'remove', 'pass', 'sub', 'option', 'list']"
        :disab-arr="[isDisabled && 'save', 'close', 'cancel', 'com1', 'com2', 'using', 'copy', 'toExcel', 'freeze']"
        :fbillno="prodValue.fbillNo"
        :list="'/assemblyList'"
        @save="insert"
        @refresh="desmountDetails"
        @submit="insert(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @del="delectHandle"
      />
      <el-button
        style="margin-left: 10px;"
        type="primary"
        size="mini"
        @click="isProofVis=true"
      >生成凭证</el-button>
    </div>
    <el-tabs v-model="activeName" class="tabs" @tab-click="handleClick">
      <el-tab-pane :label="$t('market.info')" name="first">
        <el-form ref="purchaseRef" :model="prodValue" label-width="100px" size="mini">
          <el-form-item :label="$t('market.state')">
            <el-tag :type="prodValue.stateColor">{{ prodValue.fdocumentStatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.billType')">
            <el-input v-model="prodValue.fbillTypeName" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.billNo')">
            <el-input v-model="prodValue.fbillNo" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.orderDate')">
            <el-date-picker
              v-model.trim="prodValue.fdate"
              type="date"
              value-format="yyyy-MM-dd"
              :disabled="isDisabled"
            />
          </el-form-item>
          <!--          <el-form-item label="拆卸组织">-->
          <!--            <el-input v-model.trim="prodValue.fdismorgname" size="mini" disabled />-->
          <!--          </el-form-item>-->
          <el-form-item :label="$t('market.stocker')">
            <el-autocomplete
              v-model.trim="prodValue.fstockerName"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch3"
              :disabled="isDisabled"
              @focus="querySalesperson(3)"
              @select="marketData"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="marketVisiblit=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fuserName }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item :label="$t('market.deaprt')">
            <el-autocomplete
              v-model.trim="prodValue.fdeptName"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch8"
              :disabled="isDisabled"
              @focus="getBasicUnit('SX_V_WORKSHOP')"
              @select="selUnit"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="unitVisabled=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.fince')" name="two">
        <el-form :model="prodValue" label-width="100px" size="mini">
          <el-form-item :label="$t('market.settleCurr')" prop="fsettleCurrId"> <!--结算币别-->
            <el-autocomplete
              v-model.trim="prodValue.fsettleCurr"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch5"
              size="mini"
              :disabled="isDisabled"
              @focus="queryTBdCurrency"
              @select="currencyData"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="currencyVisiblit=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item :label="$t('market.exchangeType')"><!--汇率类型-->
            <el-input v-model.trim="prodValue.fexchangeType" disabled size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.localCurr')"><!--本位币-->
            <el-input v-model.trim="prodValue.flocalCurr" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.exchangeRate')"><!--汇率-->
            <el-input v-model.trim="prodValue.fexchangeRate" disabled size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.worthall')"><!--总价zhi-->
            <el-input v-model.trim="prodValue.fworthall" disabled size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.worthalllc')"><!--总价值(本位币)-->
            <el-input v-model.trim="prodValue.fworthalllc" disabled size="mini" />
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.record1')" name="log">
        <jc-marker
          ref="log"
          :fnumber="prodValue.fbillNo"
          other-height="68vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
    <el-tabs v-if="logShow" v-model="activeName1">
      <div style="margin-bottom: 5px">
        <el-button size="mini" @click="newLine">{{ $t('btn.newHang') }}<!--新增行--></el-button>
      </div>
      <!--成品明细-->
      <el-tab-pane :label="$t('market.atherDetail')" class="tabOne" name="first">
        <jc-table
          :table-data="saleDetails"
          :table-header="saleHeader"
          table-height="200px"
          serial
          @rowClick="product"
        >
          <!--  物料编码-->
          <template v-slot:billSlot="col">
            <el-input
              v-model.trim="col.scope.row.fnumber"
              :disabled="isDisabled"
              size="mini"
              @input="getAtuo(col.scope.$index, col.scope.row.fnumber, 1)"
            >
              <i
                slot="prefix"
                class="iconfont icon-jin-rud-ao-bo"
                @click="sonJumpMateriel(col.scope.row.fnumber)"
              />
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="materia(1, col.scope.$index)"
              />
            </el-input>
            <div v-if="aotuTab && material === col.scope.$index" :class="material<7 ? 'tabPosit': 'tabPosit1'">
              <number-code :fnumber="fnumber" @material="materialData" />
            </div>
          </template>
          <!--数量-->
          <template v-slot:tagSlot="col">
            <el-input-number
              v-model="col.scope.row.fqty"
              :min="0"
              :disabled="isDisabled"
              size="mini"
            />
          </template>
          <!--仓库-->
          <template v-slot:btnState="col">
            <el-input
              v-model="col.scope.row.fstockName"
              :disabled="isDisabled"
              size="mini"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="handleWarehouse(col.scope.$index, 1)"
              />
            </el-input>
          </template>
          <!--备注-->
          <template v-slot:btnSlot="col">
            <el-input
              v-model="col.scope.row.fremark"
              size="mini"
              :disabled="isDisabled"
            />
          </template>
          <!--物料价值-->
          <template v-slot:text1Slot="col">
            <el-input-number
              v-model="col.scope.row.fworth"
              :min="0"
              size="mini"
              :disabled="isDisabled"
            />
          </template>
          <!--  库存组织  -->
          <template v-slot:text3Slot="col">
            <el-select
              v-model="col.scope.row.fstockorgid"
              :disabled="isDisabled"
              @change="col.scope.row.fownerid=col.scope.row.fstockorgid"
            >
              <el-option
                v-for="option in teamList"
                :key="option.fid"
                :label="option.fname"
                :value="option.fid"
              />
            </el-select>
          </template>
          <!--  货主  -->
          <template v-slot:text2Slot="col">
            <el-select
              v-model.trim="col.scope.row.fownerid"
              disabled
            >
              <el-option
                v-for="option in teamList"
                :key="option.fid"
                :label="option.fname"
                :value="option.fid"
              />
            </el-select>
          </template>
          <!--库位-->
          <template v-slot:text3Slot="col">
            <el-select v-model="col.scope.row.fcodefnumber" size="mini" @change="handelCode2(col.scope.$index)">
              <el-option v-for="item in col.scope.row.codes" :key="item.fid" :label="item.positionfnumber" :value="item.fnumber" />
            </el-select>
          </template>
          <!--操作-->
          <template v-slot:btnStates="col">
            <el-button type="primary" size="mini" @click="resolution(col.scope.$index)">{{ $t('market.resolution') }}<!--自动拆分--></el-button>
            <el-button type="danger" size="mini" @click="deleLine(1, col.scope.$index)">{{ $t('shop.delete') }}</el-button>
          </template>
        </jc-table>
      </el-tab-pane>
    </el-tabs>
    <el-tabs v-if="logShow" v-model="activeName1">
      <div style="margin-bottom: 5px">
        <el-button size="mini" @click="twoLine">{{ $t('btn.newHang') }}<!--新增行--></el-button>
      </div>
      <!--子件明细-->
      <el-tab-pane :label="$t('market.childDetail')" class="tabOne" name="first">
        <jc-table
          :table-data="saleDetails[index1].childs"
          :table-header="tableHeader"
          :table-height="tableHeight"
          serial
        >
          <!--  物料编码-->
          <template v-slot:billSlot="col">
            <el-input
              v-model.trim="col.scope.row.fnumber"
              :disabled="isDisabled"
              size="mini"
              @input="getAtuo(col.scope.$index, col.scope.row.fnumber, 2)"
            >
              <i
                slot="prefix"
                class="iconfont icon-jin-rud-ao-bo"
                @click="sonJumpMateriel(col.scope.row.fnumber)"
              />
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="materia(2, col.scope.$index)"
              />
            </el-input>
            <div v-if="aotuTab1 && material === col.scope.$index" :class="material<7 ? 'tabPosit': 'tabPosit1'">
              <number-code :fnumber="fnumber" @material="materialData" />
            </div>
          </template>
          <!--数量-->
          <template v-slot:tagSlot="col">
            <el-input-number
              v-model="col.scope.row.fqty"
              :min="0"
              :disabled="isDisabled"
              size="mini"
            />
          </template>
          <!--物料价值-->
          <template v-slot:text1Slot="col">
            <el-input-number
              v-model="col.scope.row.fworth"
              :min="0"
              size="mini"
              :disabled="isDisabled"
            />
          </template>
          <!--仓库-->
          <template v-slot:btnState="col">
            <el-input
              v-model="col.scope.row.fstockName"
              size="mini"
              :disabled="isDisabled"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="handleWarehouse(col.scope.$index, 2)"
              />
            </el-input>
          </template>
          <!--备注-->
          <template v-slot:btnSlot="col">
            <el-input
              v-model="col.scope.row.fremark"
              :disabled="isDisabled"
              size="mini"
            />
          </template>
          <!--  库存组织  -->
          <template v-slot:text3Slot="col">
            <el-select
              v-model="col.scope.row.fstockorgid"
              :disabled="isDisabled"
              @change="col.scope.row.fownerid=col.scope.row.fstockorgid"
            >
              <el-option
                v-for="option in teamList"
                :key="option.fid"
                :label="option.fname"
                :value="option.fid"
              />
            </el-select>
          </template>
          <!--  货主  -->
          <template v-slot:text2Slot="col">
            <el-select
              v-model.trim="col.scope.row.fownerid"
              disabled
            >
              <el-option
                v-for="option in teamList"
                :key="option.fid"
                :label="option.fname"
                :value="option.fid"
              />
            </el-select>
          </template>
          <!--库位-->
          <template v-slot:text3Slot="col">
            <el-select v-model="col.scope.row.fcodefnumber" size="mini" @change="handelCode1">
              <el-option v-for="item in col.scope.row.codes" :key="item.fid" :label="item.positionfnumber" :value="item.fnumber" />
            </el-select>
          </template>
          <!--操作-->
          <template v-slot:btnStates="col">
            <el-button type="danger" size="mini" @click="deleLine(2, col.scope.$index)">{{ $t('shop.delete') }}</el-button>
          </template>
        </jc-table>
      </el-tab-pane>
    </el-tabs>
    <!--物料弹窗-->
    <material v-if="isMateria" @material="materialData" />
    <!--销售员列表-->
    <market v-if="marketVisiblit" :staff-type="3" @market="marketData" />
    <!--  数据字典弹窗  -->
    <unit v-if="unitVisabled" msg="SX_V_WORKSHOP" @unit="selUnit" />
    <!--结算币别列表-->
    <currency v-if="currencyVisiblit" @currency="currencyData" />
    <!--仓库-->
    <selectCK
      v-if="seleceVisiblit"
      :fmaterial-id="stockFnumber"
      :msg="msg"
      @client="selectData"
    />
    <!--  生成凭证  -->
    <proof v-if="isProofVis" :fid="prodValue.fid" :from-bill-no="prodValue.fbillNo" @proof="isProofVis=false" />
  </div>
</template>
<script>
import {
  auditDismounts, delDismounts,
  notsubmitDismounts, queryChildByBom,
  queryStkDesmountDetails, submitDismounts,
  unauditDismounts, updateStkDismountDetails
} from '@/api/repertory/assembly'
import { toMxAmina } from '@/components/ToMxamineState'
import jumpMateriel from '@/components/JumpMateriel'
import mixinsImg from '@/views/market/marketManage/createMarkerOrder/components/mixinsImg'
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import { queryRate, queryViewItem } from '@/api/marketManage/marketOrder'
import jcMarker from '@/components/marker'
import jcButton from '@/components/Button'
import proof from '@/views/financial/voucherTemplate/components/proof'
import bility from '@/mixin/Operability'
import { queryTBdCodeListByFnumber } from '@/api/repertory/picking/otherDelivery'

export default {
  name: 'Assembly',
  components: {
    currency: () => import('@/views/market/marketManage/createMarkerOrder/components/currency'),
    unit: () => import('@/views/basic/createMateriel2/components/unit'),
    market: () => import('@/views/market/marketManage/createMarkerOrder/components/market'),
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material'),
    selectCK: () => import('@/views/production/collect/components/seleceCK'),
    numberCode: () => import('@/components/NumberCode'),
    jcMarker,
    jcButton,
    proof
  },
  mixins: [jumpMateriel, mixinsImg, autocomplete, bility],
  data() {
    return {
      material: 0,
      fnumber: '',
      isDisabled: false,
      activeName: 'first', // 基本信息
      activeName1: 'first', // 明细信息
      isProofVis: false,
      currencyVisiblit: false, // 币别
      unitVisabled: false, // 部门
      isMateria: false, // 物料弹窗
      seleceVisiblit: false, // 仓库
      stockFnumber: '',
      msg: '',
      fbusinessType: '1',
      marketVisiblit: false, // 仓管
      prodValue: {}, // 表单数据
      tableHeight: '250px',
      indexSelf: 0,
      index1: 0,
      otherUrlObject: {},
      finished: true, // 成品、子件
      mostatusData: [], // 单据类型
      logShow: true,
      saleDetails: [],
      saleHeader: [
        { label: this.$t('market.fseq'), prop: 'fseq', width: '80' },
        { label: this.$t('market.fmaterial'), type: 'bill', prop: 'fnumber', minWidth: '160px' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', minWidth: '140px' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecificaTion', minWidth: '120px' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '200px' },
        { label: this.$t('market.funit'), prop: 'funitName', minWidth: '100px' },
        { label: this.$t('market.fqty'), type: 'tag', prop: 'fqty', minWidth: '110px' },
        { label: this.$t('market.globalValue'), type: 'text1', prop: 'fworth', minWidth: '80px' },
        { label: this.$t('market.stock2'), type: 'state', prop: 'fstockName', minWidth: '110px' },
        { label: this.$t('market.inventoryQty'), prop: 'stockQty', minWidth: '80px' },
        { label: this.$t('market.storageLoca'), type: 'text3', prop: 'fcodefnumber', width: '100' },
        { label: this.$t('market.storageQty'), prop: 'fcodeQty', width: '80' },
        // { label: '库存组织', type: 'text3', minWidth: '80px' },
        // { label: '货主', type: 'text2', minWidth: '80px' },
        { label: this.$t('market.note'), type: 'btn', prop: 'fremark', minWidth: '200px' },
        { label: this.$t('market.handle'), type: 'states', minWidth: '150px' }
      ],
      tableHeader: [
        { label: this.$t('market.fseq'), prop: 'fseq', width: '80' },
        { label: this.$t('market.fmaterial'), type: 'bill', prop: 'Fnumber', minWidth: '160px' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', minWidth: '140px' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecificaTion', minWidth: '120px' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '200px' },
        { label: this.$t('market.funit'), prop: 'funitName', minWidth: '100px' },
        { label: this.$t('market.fqty'), type: 'tag', prop: 'fqty', minWidth: '110px' },
        { label: this.$t('market.globalValue'), type: 'text1', prop: 'fworth', minWidth: '80px' },
        { label: this.$t('market.stock2'), type: 'state', prop: 'fstockName', minWidth: '110px' },
        { label: this.$t('market.inventoryQty'), prop: 'stockQty', minWidth: '80px' },
        { label: this.$t('market.storageLoca'), type: 'text3', prop: 'fcodefnumber', width: '100' },
        { label: this.$t('market.storageQty'), prop: 'fcodeQty', width: '80' },
        // { label: '库存组织', type: 'text3', minWidth: '80px' },
        // { label: '货主', type: 'text2', minWidth: '80px' },
        { label: this.$t('market.note'), type: 'btn', prop: 'fremark', minWidth: '200px' },
        { label: this.$t('market.handle'), type: 'states', minWidth: '90px' }
      ],
      delFathers: [],
      delChilds: [],
      aotuTab: false,
      aotuTab1: false
    }
  },
  created() {
    this.business()
    if (this.$route.params.id) {
      this.desmountDetails()
    }
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 500px)' : '400px'
  },
  methods: {
    // 监听库位 库位切换
    handelCode2(index) {
      const item = this.saleDetails[index]
      item.codes.forEach(res => {
        if (item.fnumber === res.fnumber) {
          item.fcodeQty = res.fqty
        }
      })
    },
    // 监听库位 库位切换
    handelCode1(ev) {
      const item = this.saleDetails[this.index1].childs
      item.codes.forEach(res => {
        if (ev === res.fnumber) {
          item.fcodeQty = res.fqty
        }
      })
    },
    // 输入物料编码
    getAtuo(index, fnumber, ev) {
      this.indexSelf = index
      if (ev === 1) {
        this.aotuTab = true
        this.finished = true
      } else {
        this.aotuTab1 = true
        this.finished = false
      }
      this.material = index
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber = fnumber
      }, 600)
    },
    // 详情
    desmountDetails() {
      const DATA = {
        fid: this.$route.params.id
      }
      queryStkDesmountDetails(DATA).then(res => {
        if (res.code === 0) {
          this.prodValue = toMxAmina(res.data)
          this.otherUrlObject = res.data
          this.saleDetails = res.data.fathers
          this.saleDetails.map(res => {
            res.fcodeQty = res.codes?.[0]?.fqty || 0
            res.childs.forEach(ite => {
              ite.fcodeQty = ite.codes?.[0]?.fqty || 0
            })
          })
          this.isDisabled = this.prodValue.fdocumentStatus === this.$t('market.state1') ||
            this.prodValue.fdocumentStatus === this.$t('market.state2')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 修改采购订单
    async insert(ev) {
      this.saleDetails.map(res => {
        res.fwmscode = res.fcodefnumber
        res.childs.forEach(ite => {
          ite.fwmscode = ite.fcodefnumber
        })
      })
      const DATA = {
        ...this.prodValue,
        fathers: this.saleDetails,
        delFathers: this.delFathers,
        delChilds: this.delChilds
      }
      await updateStkDismountDetails(DATA).then(res => {
        if (res.code === 0) {
          this.delFathers = []
          this.delChilds = []
          ev === 1 ? this.submit(1) : this.desmountDetails()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 单据类型
    business() {
      queryViewItem({ viewName: 'SX_DISMOUNTBILLTYPE' }).then(res => {
        this.mostatusData = res.data.list
      })
    },
    // 获取仓管员数据(子传父)
    marketData(item) {
      if (item.fuserId) {
        this.prodValue.fstockerName = item.fuserName
        this.prodValue.fstockerId = item.fuserId
      }
      this.marketVisiblit = false
    },
    // 数据字典选中
    selUnit(ev) {
      if (ev.fid) { // 生产部门
        this.prodValue.fdeptId = ev.fid
        this.prodValue.fdeptName = ev.fname
      }
      this.unitVisabled = false
    },
    // 点击成品明细
    product(row) {
      this.index1 = row.index
    },
    // 打开物料弹窗
    materia(ev, index) {
      this.isMateria = true
      this.indexSelf = index
      if (ev === 1) {
        this.finished = true
      } else {
        this.finished = false
      }
    },
    // 获取物料
    materialData(ev) {
      if (ev.fnumber) {
        if (this.finished) { // 成品明细
          const item = this.saleDetails[this.indexSelf]
          item.fmodel = ev.fmodel
          item.fnumber = ev.fnumber
          item.fname = ev.fname
          item.fmaterialId = ev.fmaterialId
          item.fspecificaTion = ev.fspecificaTion
          item.fdescripTion = ev.fdescripTion
          item.funitName = ev.funit
          item.funitId = ev.funitId
          this.aotuTab = false
          this.queryTBdCodeList(item, item.fnumber)
        } else { // 子件明细
          const item = this.saleDetails[this.index1].childs[this.indexSelf]
          item.fmodel = ev.fmodel
          item.fnumber = ev.fnumber
          item.fname = ev.fname
          item.fmaterialId = ev.fmaterialId
          item.fspecificaTion = ev.fspecificaTion
          item.fdescripTion = ev.fdescripTion
          item.funitName = ev.funit
          item.funitId = ev.funitId
          this.aotuTab1 = false
          this.queryTBdCodeList(item, item.fnumber)
        }
      }
      this.isMateria = false
    },
    // 根据物料查询库位
    queryTBdCodeList(item, fnumber) {
      queryTBdCodeListByFnumber({ fnumber }).then(res => {
        if (res.code === 0) {
          // item.codes = res.data
          if (this.finished) { // 成品明细
            const item = this.saleDetails[this.indexSelf]
            item.codes = res.data
          } else {
            const item = this.saleDetails[this.index1].childs[this.indexSelf]
            item.codes = res.data
          }
        }
      })
    },
    // 打开仓库弹窗
    handleWarehouse(index, ev) {
      if (ev === 1) { // 成品
        this.finished = true
        const item = this.saleDetails[index].fnumber
        this.stockFnumber = item
      } else {
        this.finished = false
        const item = this.saleDetails[this.index1].childs[index].fnumber
        this.stockFnumber = item
      }
      this.msg = ev // 转入转出仓库
      this.indexSelf = index
      this.seleceVisiblit = true
    },
    // 获取仓库数据
    selectData(ev) {
      if (ev.fstockId) {
        this.seleceVisiblit = false
        if (this.finished) { // 成品明细
          this.saleDetails[this.indexSelf].fstockId = ev.fstockId
          this.saleDetails[this.indexSelf].fstockName = ev.fname
          this.saleDetails[this.indexSelf].stockQty = ev.sumQty
        } else { // 子件明细
          const item = this.saleDetails[this.index1].childs[this.indexSelf]
          item.fstockId = ev.fstockId
          item.fstockName = ev.fname
          item.stockQty = ev.sumQty
        }
      } else {
        this.seleceVisiblit = false
      }
    },
    // 获取结算币别数据(子传父)
    currencyData(item) {
      if (item.fsettleCurrId || item.fcurrencyId) {
        this.prodValue.fsettleCurr = item.fsettleCurrIdName || item.fname
        this.prodValue.fsettleCurrId = item.fsettleCurrId || item.fcurrencyId
        this.prodValue.flocalCurrId && this.querySalerRate()
      }
      this.currencyVisiblit = false
    },
    // 查询销售订单汇率
    async querySalerRate() {
      const DATA = {
        tocurrId: this.prodValue.flocalCurrId, // 本位币
        fromcurrId: this.prodValue.fsettleCurrId, // 结算币别
        queryDate: this.prodValue.fdate
      }
      const { data: RES, code, message } = await queryRate(DATA)
      if (code === 0) {
        this.prodValue.fexchangeRate = RES.exchangeRate
        this.prodValue.fexchangeType = RES.exchangeTypeName
        this.prodValue.fexchangeTypeId = RES.exchangeTypeId
      } else {
        this.prodValue.fexchangeRate = ''
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 成品明细拆分
    resolution(index) {
      const item = this.saleDetails[index]
      const DATA = {
        fmaterialId: item.fmaterialId,
        fqty: item.fqty
      }
      queryChildByBom(DATA).then(res => {
        if (res.code === 0) {
          this.saleDetails[this.index1].childs.push(...res.data)
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 公共函数
    toExamineFun(api) {
      this.outentitys = []
      this.inentitys = []
      this.saleDetails.forEach(res => {
        this.outentitys.push({
          fsourcebillno: this.prodValue.fbillNo,
          fcodefnumber: res.fcodefnumber,
          fmaterialid: res.fmaterialId,
          fmaterialnumber: res.fnumber,
          fmoentryid: res.fentryId,
          fqty: res.fqty,
          funitid: res.funitId || res.funitid,
          isRaw: res.isRaw
        })
        res.childs.forEach(ite => {
          this.inentitys.push({
            fsourcebillno: this.prodValue.fbillNo,
            fcodefnumber: ite.fcodefnumber,
            fmaterialid: ite.fmaterialId,
            fmaterialnumber: ite.fnumber,
            fmoentryid: ite.fentryId,
            fqty: ite.fqty,
            funitid: ite.funitId || ite.funitid,
            isRaw: ite.isRaw
          })
        })
      })
      const DATA = {
        Numbers: [this.prodValue.fbillNo],
        outentitys: this.outentitys,
        inentitys: this.inentitys,
        update: {
          ...this.prodValue,
          fathers: this.saleDetails,
          delFathers: this.delFathers,
          delChilds: this.delChilds
        }
      }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.desmountDetails()
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? submitDismounts : notsubmitDismounts
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? auditDismounts : unauditDismounts
      this.toExamineFun(api)
    },
    // 删除
    delectHandle() {
      this.$confirm('确定要删除改订单么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {
        const DATA = { Numbers: [this.prodValue.fbillNo] }
        delDismounts(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          // 跳转到修改页面
          this.$router.push({ path: '/assemblyList' })
        })
      })
    },
    // 删除行
    deleLine(ev, index) {
      if (ev === 1) {
        this.index1 = 0
        const fid = this.saleDetails[index].fentryId
        this.saleDetails.splice(index, 1)
        this.delFathers.push(fid)
        if (this.saleDetails.length === 0) {
          this.saleDetails = [
            {
              fmodel: '',
              funitName: '',
              fmaterialId: 0,
              fspecificaTion: '',
              fdescripTion: '',
              fnumber: '',
              fqty: 0,
              fprice: 0,
              fstockId: '',
              fstockName: '',
              fremark: '',
              childs: []
            }
          ]
        }
      } else {
        const fid = this.saleDetails[this.index1].childs[index].fentryId
        this.saleDetails[this.index1].childs.splice(index, 1)
        this.delChilds.push(fid)
      }
    },
    // 新增行
    newLine() {
      this.saleDetails.push({
        codes: [],
        fmodel: '',
        funitName: '',
        funitId: '',
        fmaterialId: 0,
        fspecificaTion: '',
        fdescripTion: '',
        fnumber: '',
        fqty: 0,
        fprice: 0,
        fstockId: '',
        fstockName: '',
        fremark: '',
        fstockorgid: this.prodValue.fdismorgid,
        fownerid: this.prodValue.fdismorgid,
        childs: []
      })
    },
    twoLine() {
      this.saleDetails[this.index1].childs.push({
        codes: [],
        fmodel: '',
        funitName: '',
        funitId: '',
        fmaterialId: 0,
        fspecificaTion: '',
        fdescripTion: '',
        fnumber: '',
        fqty: 0,
        fstockId: '',
        fstockName: '',
        fstockorgid: this.prodValue.fdismorgid,
        fownerid: this.prodValue.fdismorgid,
        fremark: ''
      })
    },
    // 操作记录
    async handleClick(tab) {
      if (tab.name === 'log') {
        this.logShow = false
        await this.$refs.log.otherlog()
      } else {
        this.logShow = true
      }
    }
  }
}
</script>
<style scoped lang="scss">
.content {
  @include listBom;
  .dropdown {
    display: flex;
  }
  .el-tabs {
    &::v-deep.el-tabs__header {
      margin: 0 0 5px;
    }
    .el-table{
      &::v-deep .el-input-number .el-input__inner{
        padding: 0 5px;
        width: 80px;
      }
      &::v-deep .el-input-number .el-input{
        width: 80px;
      }
      &::v-deep .el-input-number--mini{
        width: 80px;
      }
      &::v-deep .el-input-number__increase{
        display: none;
      }
      &::v-deep .el-input-number__decrease{
        display: none;
      }
      &::v-deep .el-table__footer-wrapper tbody td{
        background: #fdf6ec!important;
      }
      &::v-deep .el-input--prefix .el-input__inner {
        width: 100%;
      }
      &::v-deep .el-date-editor.el-input{
        width: 130px!important;
      }
    }
    .el-input::v-deep .el-input__inner {
      height: 30px;
      line-height: 30px;
    }
    .el-form {
      display: flex;
      flex-wrap: wrap;

      .el-form-item {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        height: 35px;
        width: 275px;
        margin-bottom: 0;
        .btnType{
          margin-right: 20px;
        }
        .el-date-editor.el-input{
          width: 100% !important;
        }
        .el-select{
          width: 100%;
        }
      }
    }
  }
}

.el-input__icon {
  cursor: pointer;
}
.tabPosit {
  padding: 5px;
  box-shadow: 0 0 2px #aaa;
  position: absolute;
  top: 3.5vh;
  left: 0;
  background-color: rgba(255, 255, 255, 1);
  z-index: 99999!important;
}
.tabPosit1 {
  padding: 5px;
  box-shadow: 0 0 2px #aaa;
  position: absolute;
  top: -25.69vh;
  left: 0;
  background-color: rgba(255, 255, 255, 1);
  z-index: 9999;
}
</style>
